package com.ling.pl.security.model;

import com.ling.pl.core.commons.annotation.ModelProperty;
import com.ling.pl.core.commons.constant.TablePreDef;
import com.ling.pl.core.model.impl.BaseEntity;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;

/**
 * 角色成员
 *
 * @author bo.wang
 * @version 1.0.0
 * @create 2016年3月15日 下午3:28:13
 */
@Entity
@Table(name = TablePreDef.BASEPRE + "ROLE_MEMBER")
public class RoleMember extends BaseEntity {

    public static final String USER = "user";
    public static final String DEPT = "org";
    public static final String POSITION = "position";
    public static final String GROUP = "group";

    @Id
    @Column(name = "ROLE_MENBER_ID", length = 36)
    String id;

    @Column(name = "ORG_ID", length = 36)
    String orgId;

    @Column(name = "POSITION_ID", length = 36)
    String positionId;

    @Column(name = "ROLE_ID", length = 36)
    String roleId;

    @Column(name = "IS_GRANTED")
    @ModelProperty(comment = "是否启用授权", des = "貌似暂时没有实现")
    boolean granted;

    @Column(name = "USERNAME", length = 36)
    String username;
    @ManyToOne(targetEntity = DefaultUser.class)
    @Cascade(CascadeType.REFRESH)
    @JoinColumn(name = "USERNAME", insertable = false, updatable = false, foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    DefaultUser user;

    @Column(name = "GROUP_ID", length = 36)
    String groupId;
    @ManyToOne(targetEntity = DefaultGroup.class)
    @Cascade(CascadeType.REFRESH)
    @JoinColumn(name = "GROUP_ID", insertable = false, updatable = false, foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    DefaultGroup group;

    @Transient
    DefaultOrg org;

    @Transient
    DefaultPosition position;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public DefaultUser getUser() {
        return user;
    }

    public void setUser(DefaultUser user) {
        this.user = user;
    }

    public DefaultOrg getOrg() {
        return org;
    }

    public void setOrg(DefaultOrg org) {
        this.org = org;
    }

    public DefaultPosition getPosition() {
        return position;
    }

    public void setPosition(DefaultPosition position) {
        this.position = position;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public DefaultGroup getGroup() {
        return group;
    }

    public void setGroup(DefaultGroup group) {
        this.group = group;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getOrgId() {
        return orgId;
    }

    public void setOrgId(String orgId) {
        this.orgId = orgId;
    }

    public String getPositionId() {
        return positionId;
    }

    public void setPositionId(String positionId) {
        this.positionId = positionId;
    }

    public boolean isGranted() {
        return granted;
    }

    public void setGranted(boolean granted) {
        this.granted = granted;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

//    @Override
//    public Serializable getBusinessId() {
//        return getId();
//    }

//    @Override
//    public String toLogString(String operationType, Log log) {
//        ///todo
//        IDao dao = SpringUtil.getBean(BaseDao.BEAN_ID);
//        String logInfo = " 关系如下:";
//        if (getUsername() != null) {
//            DefaultUser user = (DefaultUser) dao.get(DefaultUser.class, getUsername());
//            logInfo = logInfo + " 用户名:" + getUsername();
//        }
//        if (getRoleId() != null) {
//            if (dao != null) {
//                DefaultRole role = (DefaultRole) dao.get(DefaultRole.class, getRoleId());
//                if (role != null) {
//                    logInfo = logInfo + " 角色名:" + role.getName();
//                }
//            }
//            logInfo = logInfo + " 角色id:" + getRoleId();
//        }
//        if (getOrgId() != null) {
//            if (dao != null) {
//                DefaultOrg org = (DefaultOrg) dao.get(DefaultOrg.class, getOrgId());
//                if (org != null) {
//                    logInfo = logInfo + " 机构名:" + org.getOrgName();
//                }
//            }
//            logInfo = logInfo + " 机构id:" + getOrgId();
//        }
//        if (getPositionId() != null) {
//            if (dao != null) {
//                DefaultPosition position = (DefaultPosition) dao.get(DefaultPosition.class, getPositionId());
//                if (position != null) {
//                    logInfo = logInfo + " 岗位名:" + position.getName();
//                }
//            }
//            logInfo = logInfo + " 岗位id:" + getPositionId();
//        }
//        if (getGroupId() != null) {
//            if (dao != null) {
//                DefaultGroup group = (DefaultGroup) dao.get(DefaultGroup.class, getPositionId());
//                if (group != null) {
//                    logInfo = logInfo + " 分组名:" + group.getName();
//                }
//            }
//            logInfo = logInfo + " 分组id:" + getGroupId();
//        }
//        return logInfo;
//    }

}
